/**
 * 
 */
package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.MouseInfo;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JDialog;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import ac.svn.accesscontrol.ui.IUIConstants;
import ac.svn.accesscontrol.utils.Utility;



/**
 * @author Enigmanic
 *
 */
public class IntellisenseWindow extends JDialog implements IUIConstants {

	private static final long serialVersionUID = 1L;
	private IntellisensePanel intellisensePanel;
	private Border defaultBorder, focusedBorder ;
	
	public IntellisenseWindow()
	{
		super();
		setAlwaysOnTop(true);
		setUndecorated(true);
				
		
		initComponents();
		registerListeners();
		
		Utility.addEscapeListener(this);
	}

	private void registerListeners() 
	{
		addWindowFocusListener(new WindowFocusListener() 
		{
			@Override
			public void windowLostFocus(WindowEvent arg0) 
			{
				intellisensePanel.setBorder(defaultBorder);
				setVisible(false);
			}
			
			@Override
			public void windowGainedFocus(WindowEvent arg0) {
				intellisensePanel.setBorder(focusedBorder);
			}
		});
	}

	private void initComponents() 
	{
		defaultBorder		= new EtchedBorder(EtchedBorder.RAISED);
		focusedBorder	= new BevelBorder(BevelBorder.RAISED); 
	
		getContentPane().setLayout(new BorderLayout());
		
		intellisensePanel = new IntellisensePanel();
		intellisensePanel.setBorder(defaultBorder);
		
		getContentPane().add(intellisensePanel,BorderLayout.CENTER);
	}

	public void setVisible(boolean b)
	{
		if(b)
		{
			setLocation(MouseInfo.getPointerInfo().getLocation());
		}
		
		if(!b)
		{
			intellisensePanel.setBorder(defaultBorder);
		}
		super.setVisible(b);
	}

	/**
	 * @return the intellisensePanel
	 */
	public IntellisensePanel getIntellisensePanel() {
		return intellisensePanel;
	}
	
	public void setTitle(String title)
	{
		intellisensePanel.getTitleLabel().setText(title);
	}
	
	
}
